package cn.doitedu.datayi.data_service.dao;

import cn.doitedu.datayi.data_service.po.TrafficKylinCubeQueryBean;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Repository
public class KylinDemoProjectDaoImpl implements KylinDemoProjectDao {
    @Override
    public List<TrafficKylinCubeQueryBean> query() throws Exception {

       //  select province,city,region,count(1) as pv_cnt from test.kylin_fact  group by province,city,region
        Class.forName("org.apache.kylin.jdbc.Driver");


        Properties prop = new Properties();
        prop.setProperty("user", "ADMIN");
        prop.setProperty("password", "KYLIN");
        Connection connection = DriverManager.getConnection("jdbc:kylin://hdp01:7070/mydemo_project", prop);

        PreparedStatement stmt = connection.prepareStatement("select province,city,region,count(1) as pv_cnt from test.kylin_fact  group by province,city,region");
        ResultSet resultSet = stmt.executeQuery();

        ArrayList<TrafficKylinCubeQueryBean> lst = new ArrayList<>();
        while (resultSet.next()) {
            String province = resultSet.getString(1);
            String city = resultSet.getString(2);
            String region = resultSet.getString(3);
            long pv_amt = resultSet.getLong(4);
            lst.add(new TrafficKylinCubeQueryBean(province,city,region,pv_amt));
        }

        resultSet.close();
        stmt.close();
        connection.close();

        return lst;
    }
}
